#!/usr/bin/python

def process(s):
    return s.replace("_", " ")

def tag(type0, contents, **kwargs):
    def _adjust_class(key):
        if key == "klass":
            return "class"
        else: return key
    opts = " ".join('%s="%s"' % (_adjust_class(k), str(v)) 
        for (k, v) in kwargs.items() if v is not None)
    if opts:
        optsspace = " "
    else: optsspace = ""         
    return "<%s%s%s>%s</%s>" %(type0, optsspace, opts, contents, type0)

def link(text, href, **kwargs):
    """Return an anchor link tag."""
    return tag("a", text, href=href, **kwargs)

def input_tag(type0, name, **kwargs):
    return tag("input", "", type=type0, name=name, **kwargs)

def checkbox_tag(name, value, **kwargs):
    if value:
        kwargs["checked"] = "checked"
    hidden = '<input type="hidden" name="%s" value="off" />' % name
    return hidden + input_tag("checkbox", name, **kwargs)

def boolean_radio_tag(name, value, **kwargs):
    if value:
        checked = {True: "checked", False: None}
    else: checked = {True: None, False: "checked"}
    disabled = tag("input", "", name=name, value=0, 
        checked=checked[False], type="radio", **kwargs) + "Disabled"
    enabled = tag("input", "", name=name, value=1, 
        checked=checked[True], type="radio", **kwargs) + "Enabled"
    return disabled + enabled

def list_entry(name, value, get_input_html):
    remove_link = link("Remove", "#", onclick="remove(this); return false;") 
    return get_input_html(value) + remove_link

def text_list(name, values, get_input_html, html_list_entry, **kwargs):
    contents = "".join(tag("div", list_entry(name, value, get_input_html)) 
        for value in values)
    escaped_contents = list_entry('${name}', '', 
        get_input_html).replace('"', '&quot;')
    onclick = """add(this, '%s', '', '%s'); return false;""" % \
        (name, escaped_contents)
    add_link = link("Add", "#", onclick=onclick)
    hidden = '<input type="hidden" name="%s" value="" />' % name
    return hidden + tag("div", contents) + add_link

def button_tag(caption, name, value):
    return tag("button", caption, type="submit", value=value, name=name)

def select_tag(name, values, selected, **kwargs):
    def gethtml(value):
        if value == selected:
            return tag("option", process(value),
                value=value, selected="selected")
        return tag("option", process(value), value=value)
    options = map(gethtml, values)
    return tag("select", "\n".join(options), name=name, **kwargs)

def radio_tag(name, values, selected, **kwargs):
    dchecked = {True: "checked", False: None}
    def gethtml(value):        
        return tag("input", process(value), type="radio", 
            value=value, name=name, checked=dchecked[value == selected])
    return "".join(map(gethtml, values))

def add_class_error(html, klass="field-error"):
    if html.startswith("<div"):
        return html[:4] + ' class="%s"' % klass + html[4:].strip()
    return tag("span", html, klass=klass)
